//
//  QuestionView.m
//  yameishe3
//
//  Created by pro on 2017/3/7.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "QuestionView.h"
#import "YMSMemberManager.h"
@interface QuestionView ()
{
    
    __weak IBOutlet UIView *questionView1;
    __weak IBOutlet UIView *questionView2;
    __weak IBOutlet UIView *questionView3;
    
    __weak IBOutlet UIButton *questionBtn1;
    __weak IBOutlet UIButton *questionBtn2;
    __weak IBOutlet UIButton *questionBtn3;
    
    __weak IBOutlet UITextField *answerTF1;
    __weak IBOutlet UITextField *answerTF2;
    __weak IBOutlet UITextField *answerTF3;
    
    __weak IBOutlet UILabel *accountLab;
    
    
    NSMutableDictionary *_submitDic;
    NSDictionary *_choiceQuestionDic;
    
    
    NSArray *_keyArray;
}

@end
@implementation QuestionView
-(void)awakeFromNib
{
    _keyArray = [NSArray arrayWithObjects:@"hobby",@"home",@"education", nil];
    _choiceQuestionDic = [[NSMutableDictionary alloc]init];
    accountLab.text = [YMSMember loginMember].username;
    _submitDic = [[NSMutableDictionary alloc]init];
    [_submitDic setValue:[YMSMember loginMember].token forKey:@"_token"];
    [_submitDic setValue:@"security" forKey:@"type"];
}
-(void)setQuestionDic:(NSMutableDictionary *)questionDic
{
    _questionDic = questionDic;
    for (int i = 0; i<_keyArray.count; i++) {
         NSArray*rangeArray = [_questionDic objectForKey:_keyArray[i]];
        [self setRangeViewIndex:[NSNumber numberWithInt:i] data:rangeArray];
    }
    
}
-(void)setQuestionArray:(NSMutableArray *)questionArray
{
    _questionArray = questionArray;
    for (int i = 0; i<_questionArray.count;i++) {
        NSDictionary *dic = _questionArray[i];
        UIButton *btn = [self viewWithTag:1000+i];
        [btn setTitle:dic[@"question_name"] forState:UIControlStateNormal];
        
    }
}
+(CGFloat)viewHeight
{
    return 515;
}
-(void)setRangeViewIndex:(NSNumber*)index data:(NSArray*)data
{
     UIView *_typeView = nil;
    switch ([index intValue]) {
        case 0:
            _typeView = questionView1;
            break;
        case 1:
            _typeView = questionView2;
            break;
        default:
            _typeView = questionView3;
            break;
    }
    for (int i = 0; i<data.count; i++) {
        UIButton *btn = [_typeView viewWithTag:i+100];
        if ([btn isKindOfClass:[UIButton class]]) {
              [btn setTitle:data[i][@"question_name"] forState:UIControlStateNormal];
        }
    }
}
-(IBAction)onTouchChoiceTypeBtn:(UIButton*)sender
{
    UIView *_typeView = nil;
    switch (sender.tag) {//判断为第几个分类问题
        case 0:
        {
            _typeView = questionView1;
            questionBtn2.selected = questionBtn3.selected = NO;
            [self setViewAlpha:questionView2];
            [self setViewAlpha:questionView3];
        }
            break;
        case 1:
        {
            _typeView = questionView2;
            questionBtn1.selected = questionBtn3.selected = NO;
            [self setViewAlpha:questionView1];
            [self setViewAlpha:questionView3];
        }
            break;
        default:
        {
            _typeView = questionView3;
            questionBtn2.selected = questionBtn1.selected = NO;
            [self setViewAlpha:questionView1];
            [self setViewAlpha:questionView2];
        }
            break;
    }

    if (sender.selected == YES) {//关闭选择
        [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            _typeView.alpha = 0;
        } completion:nil];
    }
    else//开启选择
    {
        [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            _typeView.alpha = 1;
        } completion:nil];
    }
    sender.selected = !sender.selected;
}

-(IBAction)onTouchTypeBtn:(UIButton*)sender
{
    UIView *_typeView = nil;
    UIButton *_choiceTypeBtn = nil;
    switch (sender.superview.tag) {//判断为第几个分类问题
        case 0:
        {
            _typeView = questionView1;
            _choiceTypeBtn = questionBtn1;
        }
            break;
        case 1:
        {
            _typeView = questionView2;
            _choiceTypeBtn = questionBtn2;
        }
            break;
        default:
        {
            _typeView = questionView3;
            _choiceTypeBtn = questionBtn3;
        }
            break;
    }
    NSArray *tempArray = [_questionDic objectForKey:_keyArray[sender.superview.tag]];
    [_choiceQuestionDic setValue:tempArray[sender.tag-100][@"id"] forKey:[NSString stringWithFormat:@"%ld",sender.superview.tag]];
    [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        _typeView.alpha = 0;
    } completion:nil];
    _choiceTypeBtn.selected = NO;
    [_choiceTypeBtn setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
-(void)setViewAlpha:(UIView*)view
{
    [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        view.alpha = 0;
    } completion:nil];
}

-(IBAction)onTouchSubmit:(id)sender
{
    if ([_choiceQuestionDic allKeys].count<3) {
        return;
    }
    NSMutableArray *questionArray = [[NSMutableArray alloc]init];
    for (int i = 0; i<_keyArray.count; i++) {
        [questionArray addObject:_choiceQuestionDic[[NSString stringWithFormat:@"%d",i]]];
    }
    NSMutableArray *answerArray = [[NSMutableArray alloc]init];
    [answerArray addObject:answerTF1.text];
    [answerArray addObject:answerTF2.text];
    [answerArray addObject:answerTF3.text];
    
    [_submitDic setValue:questionArray forKey:@"question"];
    [_submitDic setValue:answerArray forKey:@"answer"];
    [[YMSMemberManager alloc]MemberUpdateInfoWithDic:_submitDic successBlock:^(NSDictionary *result) {
        [self.viewController.navigationController popViewControllerAnimated:YES];
    } failureBlock:^(NSString *error) {
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
